var game = new Phaser.Game(800, 600, Phaser.AUTO, '', {
	preload : preload,
	create : create,
	update : update
});
//var score = 0;
//var scoreText;
var num;


//spawn
var SpawnDelay = 1000;
var SpawnDelay2 = 1000;
var SpawnDelay3 = 1000;
var SpawnDelayTime = 0;
var SpawnDelayTime2 = 0;
var SpawnDelayTime3 = 0;
var status2 = 1;
var status = 1;
//group
var group1;
var group2;

//text
var text;

//button

var button;



function preload() {

	

	game.load.spritesheet('road', 'assets/road3.png', 800, 600);
	game.load.image('platform', 'assets/road2.png');
	game.load.image('car2', 'assets/redcar.png');
	game.load.image('car', 'assets/yellowcar.png');
	game.load.image('white', 'assets/white.png');
	game.load.image('button', 'assets/restart.png');
	game.load.image('first', 'assets/firstaid.png');


}

function create() {
	
	





	// A simple background for our game
	bg = game.add.sprite(0, 0, 'road');
	white2 = game.add.group();
create3();

	stars = game.add.group();
	create2();
	
	

	
	platforms = game.add.group();

	var ledge = platforms.create(162, 1, 'platform');
	ledge.body.immovable = true;
	ledge = platforms.create(565, 1, 'platform');
	ledge.body.immovable = true;

	player = game.add.sprite(500, 700, 'car');
	player.body.collideWorldBounds = true;

cursors = game.input.keyboard.createCursorKeys();
button = game.add.button(0, 500, 'button', actionOnClick, this);


	}


function update() {


	game.physics.collide(player, platforms);




	player.body.velocity.x = 0;
	player.body.velocity.y = 0;

	if (cursors.left.isDown) {
		
		player.body.velocity.x = -500;
		

	}
	if (cursors.right.isDown) {
		
		player.body.velocity.x = 500;
		
	}

	if (cursors.up.isDown) {
		
		player.body.velocity.y = -500;

	}
	if (cursors.down.isDown) {

		player.body.velocity.y = 500;

	}



	SpawnDelayTime2 += 50;
	if (SpawnDelayTime2 > SpawnDelay2 && status != 0) {

		create3();

		SpawnDelayTime2 = 0;
	}
	
	

	
	
	SpawnDelayTime += 10;
	if (SpawnDelayTime > SpawnDelay && status != 0) {

		create2();

		SpawnDelayTime = 0;
	}
	
	if (status==0){
		     text = game.add.text(game.world.centerX, game.world.centerY, 'END', { fontSize: '32px', fill: '#ff0044' });
		    


	}


	game.physics.overlap(player, stars, carhit, null, this);
}

function carhit(player, car2) {
	// Removes the car2 from the screen
	car2.kill();
	// Add and update the score
	player.kill();

	status = 0;
}

	function create2() {

		for (var i = 0; i < 5; i++) {

			random();
			// Create a star inside of the 'stars' group
			var car2 = stars.create(280 + i * 60, 0, 'car2');
			 stars.add(car2);
			// Let gravity do its thing
			car2.body.velocity.y = +num + 10;
			// This just gives each star a slightly random bounce value
			// star.body.bounce.y = 0.7 + Math.random() * 0.2;

		}
	}

function create3() {
	for (var i = 0; i < 30; i++){

			white = game.add.sprite(418, 0, 'white');
			 white2.add(white);
	white.body.velocity.y = 500;
	
	}

}

function random() {
	num = 280 + (Math.random() * (550 - 280));
}

function actionOnClick() {
 location.reload();
}

